package com.ruoyi.mx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mx.domain.MxUser;
import com.ruoyi.mx.service.IMxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 美秀微信用户Controller
 *
 * @author ruoyi
 * @date 2021-09-16
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mx/user" )
@Api(tags = "用户模块")
public class MxUserController extends BaseController {

    private final IMxUserService iMxUserService;

    /**
     * 查询美秀微信用户列表
     */
    @PreAuthorize("@ss.hasPermi('mx:user:list')")
    @GetMapping("/list")
    @ApiOperation(value = "用户列表", notes = "获取用户列表")
    public TableDataInfo list(MxUser mxUser)
    {
        startPage();
        LambdaQueryWrapper<MxUser> lqw = new LambdaQueryWrapper<MxUser>();
        if (StringUtils.isNotBlank(mxUser.getOpenid())){
            lqw.eq(MxUser::getOpenid ,mxUser.getOpenid());
        }
        if (StringUtils.isNotBlank(mxUser.getNickname())){
            lqw.like(MxUser::getNickname ,mxUser.getNickname());
        }
        if (mxUser.getSex()!=null){
            lqw.eq(MxUser::getSex ,mxUser.getSex());
        }
        if (StringUtils.isNotBlank(mxUser.getAvatar())){
            lqw.eq(MxUser::getAvatar ,mxUser.getAvatar());
        }
        if (StringUtils.isNotBlank(mxUser.getCity())){
            lqw.eq(MxUser::getCity ,mxUser.getCity());
        }
        if (StringUtils.isNotBlank(mxUser.getSign())){
            lqw.eq(MxUser::getSign ,mxUser.getSign());
        }
        if (mxUser.getDisabled()!=null){
            lqw.eq(MxUser::getDisabled ,mxUser.getDisabled());
        }
        if (mxUser.getLastLoginTime() != null){
            lqw.eq(MxUser::getLastLoginTime ,mxUser.getLastLoginTime());
        }
        if (mxUser.getCheats() != null){
            lqw.eq(MxUser::getCheats ,mxUser.getCheats());
        }
        if (mxUser.getDeleted() != null){
            lqw.eq(MxUser::getDeleted ,mxUser.getDeleted());
        }
        List<MxUser> list = iMxUserService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出美秀微信用户列表
     */
    @PreAuthorize("@ss.hasPermi('mx:user:export')" )
    @Log(title = "美秀微信用户" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public R export(MxUser mxUser) {
        LambdaQueryWrapper<MxUser> lqw = new LambdaQueryWrapper<MxUser>(mxUser);
        List<MxUser> list = iMxUserService.list(lqw);
        ExcelUtil<MxUser> util = new ExcelUtil<MxUser>(MxUser. class);
        return util.exportExcel(list, "user" );
    }

    /**
     * 获取美秀微信用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('mx:user:query')" )
    @GetMapping(value = "/{id}" )
    public R getInfo(@PathVariable("id" ) String id) {
        return R.success(iMxUserService.getById(id));
    }

    /**
     * 新增美秀微信用户
     */
    @PreAuthorize("@ss.hasPermi('mx:user:add')" )
    @Log(title = "美秀微信用户" , businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody MxUser mxUser) {
        return toAjax(iMxUserService.save(mxUser) ? 1 : 0);
    }

    /**
     * 修改美秀微信用户
     */
    @PreAuthorize("@ss.hasPermi('mx:user:edit')" )
    @Log(title = "美秀微信用户" , businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody MxUser mxUser) {
        return toAjax(iMxUserService.updateById(mxUser) ? 1 : 0);
    }

    /**
     * 删除美秀微信用户
     */
    @PreAuthorize("@ss.hasPermi('mx:user:remove')" )
    @Log(title = "美秀微信用户" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public R remove(@PathVariable String[] ids) {
        return toAjax(iMxUserService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
